<?php

namespace App\Admin\Actions\Grid;

use App\Models\Posts\WxPost;
use App\Wen\Utils\UserUtils;
use Dcat\Admin\Actions\Response;
use Dcat\Admin\Grid\BatchAction;
use Dcat\Admin\Traits\HasPermissions;
use Illuminate\Contracts\Auth\Authenticatable;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\DB;

class PostDeleteAction extends BatchAction
{
    /**
     * @return string
     */
    protected $title = '批量删除';

    /**
     * Handle the action request.
     *
     * @param Request $request
     *
     * @return Response
     */
    public function handle(Request $request)
    {
        $ids = $this->getKey();

        foreach ($ids as $id){
            DB::transaction(function () use ($id){
                $uid = WxPost::where('id',$id)->value('user_id');
                WxPost::where('id',$id)->update(['deleted_at'=>1]);
                // 发送通知
                UserUtils::add_user_notice(2004, $uid,'笔记被删除通知','您好，您发表的笔记id为'.$id.'被管理员删除',100);
            });
        }


        return $this->response()
            ->success('操作成功'.json_encode($this->getKey()))
            ->refresh();
    }

    /**
     * @return string|array|void
     */
    public function confirm()
    {
        return ['真的要删除吗？可恢复不了'];
    }

    /**
     * @param Model|Authenticatable|HasPermissions|null $user
     *
     * @return bool
     */
    protected function authorize($user): bool
    {
        return true;
    }

    /**
     * @return array
     */
    protected function parameters()
    {
        return [];
    }
}
